{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9486bd26",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_7684/3777265428.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\dell\\AppData\\Local\\Temp/ipykernel_7684/3777265428.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def calXNew (100)\u001b[0m\n\u001b[1;37m                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "621ce9d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calXNew(t):\n",
    "    XOld=10\n",
    "    return (t /(2* XOld))+(XOld / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c42aeaf7",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def ROOT(X):\n",
    "    XOld = 1\n",
    "    i=0\n",
    "    t=2\n",
    "    while True:\n",
    "        XNew=calXNew(t)\n",
    "        a = abs(XNew-XOld)\n",
    "        XOld=XNew\n",
    "        i += 1\n",
    "        print(\"次数：\",i,\"结果：\",XNew,\"差值：\",a)\n",
    "        if a < 0.00000000001:\n",
    "            break\n",
    "    print (\"最终结果：\",XNew,\"运行次数：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a8fff407",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "次数： 1 结果： 5.1 差值： 4.1\n",
      "次数： 2 结果： 5.1 差值： 0.0\n",
      "最终结果： 5.1 运行次数： 2\n"
     ]
    }
   ],
   "source": [
    "ROOT (2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5ab0bd2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calXnew(Xold):\n",
    "    return (1 / (Xold+1))+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b73cb600",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11 1.4142135731001355\n"
     ]
    }
   ],
   "source": [
    "Xold = 0\n",
    "Xnew = 0\n",
    "i = 0\n",
    "difference = 1\n",
    " \n",
    "while difference > 0.0000001:\n",
    "    Xnew = calXnew(Xold)\n",
    "    difference = abs(Xnew - Xold)\n",
    "    Xold = Xnew\n",
    "    i += 1\n",
    "print(i, Xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7692531b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
